package com.estar.integaration.strategy;

import com.estar.integaration.entity.paymentPlanEntity.PurchaseOrderDetail;
import freemarker.template.SimpleDate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author: YJL
 * Date: 2022/2/18 20:09
 **/
abstract class Strategy {
  /**
   * 设置加上账期之后最终的期待时间
   * 设置应付的钱的字段
   * @param purchaseOrderDetailList
   */
  public abstract Boolean setExpectDate(List<PurchaseOrderDetail> purchaseOrderDetailList,PurchaseOrderDetail purchaseOrderDetail,String type);


  /**
   * 日期加上账期得到最终的String的日期
   * @param curDate
   * @param realNeedAddDay
   * @return
   */
  public String dateAddAccountPeriod(String curDate,String realNeedAddDay){
    Integer needAddDay = 0;
    if(realNeedAddDay == null)
      realNeedAddDay = "0";
    needAddDay = getRealNeedAddDay(realNeedAddDay);
    if(needAddDay == -1){
      //说明出现了特殊字符 暂时先用0
      needAddDay = 0;
    }
    if(curDate == null || curDate.equals("有到票台账但无日期"))
      return "有到票台账但无日期";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-d");
    curDate = curDate.replaceAll("/","-");
    Date date = null;
    try {
      date = simpleDateFormat.parse(curDate);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    //日期加天数
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    calendar.add(calendar.DATE,needAddDay);
    date = calendar.getTime();
    //转换成字符串回去
    curDate = simpleDateFormat.format(date);
    curDate = curDate.replace("-","/");
    return curDate;
  }

  Integer getRealNeedAddDay(String realNeedAddDay){
    realNeedAddDay = realNeedAddDay.trim();
    Pattern pattern = Pattern.compile("[0-9]*");
    Matcher isNum = pattern.matcher(realNeedAddDay);
    if(!isNum.matches()){
      return -1;
    }
    return Integer.parseInt(realNeedAddDay);
  }

}
